<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问题id" prop="questionId">
        <el-input
            v-model="queryParams.questionId"
            placeholder="请输入互动问题问题id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回答者id" prop="userId">
        <el-input
            v-model="queryParams.userId"
            placeholder="请输入回答者id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐藏状态" prop="hidden">
        <el-select v-model="queryParams.hidden" placeholder="请选择隐藏状态" clearable style="width: 160px">
          <el-option
              v-for="dict in hidden2"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="匿名状态" prop="anonymity">
        <el-select v-model="queryParams.anonymity" placeholder="请选择匿名状态" clearable style="width: 160px">
          <el-option
              v-for="dict in anonymity2"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
            v-model="daterangeCreateTime"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="interaction_replyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论id" align="center" prop="id" />
      <el-table-column label="问题id" align="center" prop="questionId" />
      <el-table-column label="回复的上级回答id" align="center" prop="answerId" v-if="false"/>
      <el-table-column label="回答者id" align="center" prop="userId" />
      <el-table-column label="回答内容" align="center" prop="content" />
      <el-table-column label="回复的目标用户id" align="center" prop="targetUserId" v-if="false"/>
      <el-table-column label="回复的目标回复id" align="center" prop="targetReplyId" v-if="false"/>
      <el-table-column label="评论数量" align="center" prop="replyTimes" />
      <el-table-column label="点赞数量" align="center" prop="likedTimes" />
      <el-table-column label="隐藏状态" align="center" prop="hidden">
        <template #default="scope">
          <dict-tag :options="hidden2" :value="scope.row.hidden"/>
        </template>
      </el-table-column>
      <el-table-column label="匿名状态" align="center" prop="anonymity">
        <template #default="scope">
          <dict-tag :options="anonymity2" :value="scope.row.anonymity"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['study:interaction_reply:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['study:interaction_reply:remove']">删除</el-button>
        </template>
      </el-table-column>

    </el-table>


    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改互动问题的回答或评论对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="interaction_replyRef" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="问题id" prop="questionId">
          <el-input v-model="form.questionId" placeholder="请输入互动问题问题id" />
        </el-form-item>
        <el-form-item label="评论id" prop="answerId">
          <el-input v-model="form.answerId" placeholder="请输入回复的上级回答id" />
        </el-form-item>
        <el-form-item label="评论者id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入回答者id" />
        </el-form-item>
        <el-form-item label="回答内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="回复的目标用户id" prop="targetUserId">
          <el-input v-model="form.targetUserId" placeholder="请输入回复的目标用户id" />
        </el-form-item>
        <el-form-item label="回复的目标回复id" prop="targetReplyId">
          <el-input v-model="form.targetReplyId" placeholder="请输入回复的目标回复id" />
        </el-form-item>
        <el-form-item label="隐藏状态" prop="hidden">
          <el-radio-group v-model="form.hidden">
            <el-radio
                v-for="dict in hidden2"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="匿名状态" prop="anonymity">
          <el-radio-group v-model="form.anonymity">
            <el-radio
                v-for="dict in anonymity2"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Interaction_reply">
import { listInteraction_reply, getInteraction_reply, delInteraction_reply, addInteraction_reply, updateInteraction_reply } from "@/api/study/interaction_reply";
import { listInteraction_question, getInteraction_question, addInteraction_question, delInteraction_question, updateInteraction_question} from "@/api/study/interaction_question.js";

const { proxy } = getCurrentInstance();
const { anonymity2, hidden2 } = proxy.useDict('anonymity2', 'hidden2');

const interaction_replyList = ref([]);
const interaction_questionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);
const daterangeUpdateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    questionId: null,
    userId: null,
    content: null,
    hidden: null,
    anonymity: null,
    createTime: null,
    description: null
  },
  rules: {
    questionId: [
      { required: true, message: "互动问题问题id不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "回答者id不能为空", trigger: "blur" }
    ],
    content: [
      { required: true, message: "回答内容不能为空", trigger: "blur" }
    ],
    replyTimes: [
      { required: true, message: "评论数量不能为空", trigger: "blur" }
    ],
    likedTimes: [
      { required: true, message: "点赞数量不能为空", trigger: "blur" }
    ],
    hidden: [
      { required: true, message: "隐藏状态不能为空", trigger: "change" }
    ],
    anonymity: [
      { required: true, message: "匿名状态不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询互动问题的回答或评论列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0];
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1];
  }
  listInteraction_reply(queryParams.value).then(response => {
    interaction_replyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  listInteraction_question(queryParams.value).then(response => {
    interaction_questionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    questionId: null,
    answerId: null,
    userId: null,
    content: null,
    targetUserId: null,
    targetReplyId: null,
    replyTimes: null,
    likedTimes: null,
    hidden: null,
    anonymity: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("interaction_replyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  daterangeUpdateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加互动问题的回答或评论";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInteraction_reply(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改互动问题的回答或评论";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["interaction_replyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInteraction_reply(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInteraction_reply(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
  proxy.$refs["interaction_questionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInteraction_question(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInteraction_question(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除互动问题的回答或评论编号为"' + _ids + '"的数据项？').then(function() {
    return delInteraction_reply(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('study/interaction_reply/export', {
    ...queryParams.value
  }, `interaction_reply_${new Date().getTime()}.xlsx`)
}

getList();
</script>
